package com.adolph.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 〈功能概述〉<br>
 *
 * @author: wangxiangjun
 * @date: 2020-10-01 9:33
 */

public class NIOFileChannel {

    public static void main(String[] args) throws IOException {
        String temp = "测试数据";

        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file.txt");
        //创建通道
        FileChannel channel = fileOutputStream.getChannel();
        //创建缓冲区
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        //写入缓冲区
        allocate.put(temp.getBytes());
        //读写转换
        allocate.flip();
        //写入数据
        channel.write(allocate);
        //关闭流
        fileOutputStream.close();
    }
}
